package cmj.cloudmusic.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import cmj.cloudmusic.R;
import cmj.cloudmusic.entity.MusicList;

/**
 * 歌单
 * Created by 王虎 on 2016/12/11.
 */
public class MusicListAdapter extends BaseAdapter {

    Context context;
    List<MusicList> list;

    public MusicListAdapter(Context context, List<MusicList> list) {
        this.context = context;
        if (list.size()>6){
            this.list=new ArrayList<>();
            this.list.add(list.get(0));
            this.list.add(list.get(1));
            this.list.add(list.get(2));
            this.list.add(list.get(3));
            this.list.add(list.get(4));
            this.list.add(list.get(5));
        }else {
            this.list=list;
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.musiclist_item,null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        MusicList musicList=list.get(position);
        viewHolder.update(musicList);
        return convertView;
    }
    public class ViewHolder{
        ImageView imageView;
        TextView title;
        TextView count;

        public ViewHolder(View view) {
            this.imageView = (ImageView) view.findViewById(R.id.img);
            this.title = (TextView) view.findViewById(R.id.title);
            this.count = (TextView) view.findViewById(R.id.count);
        }
        public void update(MusicList musicList){
            Glide.with(context).load(musicList.getImageurl()).into(imageView);
            title.setText(musicList.getTitle());
            int count_=musicList.getListencount();
            if (count_/10000>0){
                count.setText(count+"万");
            }else {
                count.setText(count_+"");
            }
        }
    }
}
